package adb.group.entity;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String userID = request.getParameter("username");
		String password = request.getParameter("password");
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Connection connection = (Connection) DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:ORCL", "dbfp_dba",
					"dbfpdba");
			Statement statement = (Statement) connection.createStatement();
			ResultSet resultSet = statement
					.executeQuery("select * from user_t where user_id='"
							+ userID + "' and password='" + password + "'");
			if (resultSet == null) {
				response.sendRedirect("/project/resources/common/login.htm");
			} else {
				while (resultSet.next()) {
					String role = resultSet.getString(3);
					String personID = resultSet.getString(4);
					
					request.getSession().setAttribute("accountID", userID);
					request.getSession().setAttribute("personID", personID);
					request.getSession().setAttribute("role", role);
					if(role.equals("physician")){
						ResultSet personResult = statement
								.executeQuery("select * from person_t where person_ID="
										+ personID);
						String name = null;
						while (personResult.next()) {
							name = personResult.getString(2) + " "
									+ personResult.getString(3);
						}
						request.getSession().setAttribute("name", name);
						
						ResultSet phResult=statement.executeQuery("select * from physician_t where person_ID="+personID);
						String phID=null;
						while(phResult.next()){
							phID=phResult.getString(1);
						}
						request.getSession().setAttribute("workID", phID);
						
						
						response.sendRedirect("/project/resources/physician/physicianmain.jsp");
					}else if(role.equals("nurse")){
						response.sendRedirect("/project/resources/common/workunit.jsp");
					}else if(role.equals("staff")){
						response.sendRedirect("/project/resources/common/workunit.jsp");
					}else if(role.equals("technician")){
						response.sendRedirect("/project/resources/common/workunit.jsp");
					}else if(role.equals("volunteer")){
						response.sendRedirect("/project/resources/common/workunit.jsp");
					}else if(role.equals("patient")){
						ResultSet personResult = statement
								.executeQuery("select * from person_t where person_ID="
										+ personID);
						String name = null;
						while (personResult.next()) {
							name = personResult.getString(2) + " "
									+ personResult.getString(3);
						}
						request.getSession().setAttribute("name", name);
						
						ResultSet paResult=statement.executeQuery("select * from patient_t where person_ID="+personID);
						String paID=null;
						while(paResult.next()){
							paID=paResult.getString(1);
						}
						request.getSession().setAttribute("workID", paID);
						response.sendRedirect("/project/resources/patient/patientmain.jsp");
						
					}else{
						response.sendRedirect("/project/resources/common/login.htm");
					}
					
					
				}
			}
		} catch (Exception e) {
			System.out.print(e);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
